package com.taxshare.manager.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.manager.entity.SupplierInfoEntity;
import com.taxshare.manager.mapper.SupplierInfoMapper;
import com.taxshare.manager.view.vo.SupplierInfoVO;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;

/**
 * @ClassName: SupplierInfoService
 * @Description:
 * @author: luozhongyi
 * @date: 2021/1/14 11:30
 */
@Service
public class SupplierInfoService extends ServiceImpl<SupplierInfoMapper, SupplierInfoEntity> {

	/**
	 * @Title: queryPageList
	 * @Description: (接口列表分页)
	 * @param @param query
	 * @param @return 参数
	 * @return Response<IPage<ApiInfoVO>> 返回类型
	 * @throws
	 */
	public Response<IPage<SupplierInfoVO>> queryPageList(PageRequest<SupplierInfoVO> query) {
		QueryWrapper<SupplierInfoEntity> queryWrapper = new QueryWrapper<>();
		String supplierCode = query.getInfo().getSupplierCode();
		if (StringUtils.isNotEmpty(supplierCode)) {
			queryWrapper.like("supplier_code", supplierCode);
		}
		String supplierName = query.getInfo().getSupplierName();
		if (StringUtils.isNotEmpty(supplierName)) {
			queryWrapper.like("supplier_name", supplierName);
		}
		String supplierTaxno = query.getInfo().getSupplierTaxno();
		if (StringUtils.isNotEmpty(supplierTaxno)) {
			queryWrapper.like("supplier_taxno", supplierTaxno);
		}
		queryWrapper.orderByDesc("create_time");
		IPage<SupplierInfoVO> result = PageHelp.siamplePage(this, queryWrapper, query.getPage(), SupplierInfoVO.class);
		return Response.success(result);
	}


	/**
	 * @Title: add
	 * @Description: (新增接口)
	 * @param @param commonCodeInfoDTO
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response saveSupplier(SupplierInfoVO supplierInfoVO) {
		SupplierInfoEntity apiEntity = DozerBeanUtil.map(supplierInfoVO, SupplierInfoEntity.class);
		apiEntity.setUuid(UUIDUitl.UUID32(false));
		apiEntity.setCreateTime(new Date());
		apiEntity.setUpdateTime(new Date());
		this.save(apiEntity);
		return Response.success();
	}
	

	/**
	 * @Title: udpateSupplier
	 * @Description: (接口修改)
	 * @param @param apiInfoVO
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response udpateSupplier(SupplierInfoVO supplierInfoVO) {
		SupplierInfoEntity apiEntity = DozerBeanUtil.map(supplierInfoVO, SupplierInfoEntity.class);
		apiEntity.setUpdateTime(new Date());
		return Response.success(this.updateById(apiEntity));
	}

	/**
	 * @Title: delete
	 * @Description: (接口删除)
	 * @param @param ids
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response deleteSupplier(String uuid) {
		return Response.success(this.removeById(uuid));
	}

	public Response<List<SupplierInfoVO>> queryList() {
		QueryWrapper<SupplierInfoEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		List<SupplierInfoEntity> list = this.list(queryWrapper);
		return Response.success(DozerBeanUtil.mapList(list, SupplierInfoVO.class));
	}
}
